<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="分组名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分组名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-260px"
        />
      </el-form-item>
    </el-form>
    <div class="flex justify-between">
      <div>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['crm:label-set:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </div>
    </div>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="listData">
      <el-table-column label="分组编号" align="center" prop="id" />
      <el-table-column label="分组名称" align="center" prop="name" />
      <el-table-column label="标签" align="left" prop="name">
        <template #default="scope">
          <div class="cusomerTag">
            <el-check-tag
              v-for="tag in scope.row.labelList"
              :key="tag.id"
              checked
              :style="{ background: tag.bgColor, color: tag.textColor, marginRight: '10px' }"
              @click="openFormTag('update', tag.id)"
              class='mb-4px inline-block'
              >{{ tag.name }}</el-check-tag
            >
            <el-button
              link
              type="primary"
              v-hasPermi="['crm:label:create']"
              @click="openFormTag('create', scope.row.id)"
            >
              新增
            </el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['crm:label-set:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crm:label-set:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="totals"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CustomerTagConfigFrom ref="formRef" @success="getList" />
  <!-- 标签弹窗：添加/修改 -->
  <TagConfigFrom ref="formTagRef" @success="getList" />
</template>

<script setup lang="ts">
import draggable from 'vuedraggable'
import { dateFormatter } from '@/utils/formatTime'
import * as customerTagApi from '@/api/crm/customerTagConfig/index'
import CustomerTagConfigFrom from './customerTagConfigFrom.vue'
import TagConfigFrom from './tagConfigFrom.vue'
import { color } from 'echarts'

defineOptions({ name: 'CustomerTagConfig' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const listData = ref<any[]>([]) // 列表的数据
const totals = ref(0)
const queryParams = reactive({
  name: null,
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const { list, total } = await customerTagApi.getCustomerTagPage(queryParams)
    listData.value = list
    totals.value = total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const formTagRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await customerTagApi.deletelabel(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
/*标签集新增*/
const openFormTag = (type: string, id?: number) => {
  formTagRef.value.open(type, id)
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style scoped lang="scss">
::v-deep(.el-check-tag) {
  padding: 1px 8px 3px;
  font-size: 12px;
}
</style>
